angular.module("DirectiveModule", ["UtilityModule"]).directive('rextoyWait', function (log) {
    var opts = {
        lines: 8, // The number of lines to draw
        length: 8, // The length of each line
        width: 5, // The line thickness
        radius: 10, // The radius of the inner circle
        corners: 0.8, // Corner roundness (0..1)
        rotate: 0, // The rotation offset
        direction: 1, // 1: clockwise, -1: counterclockwise
        color: '#555', // #rgb or #rrggbb or array of colors
        speed: 1.5, // Rounds per second
        trail: 50, // Afterglow percentage
        shadow: true, // Whether to render a shadow
        hwaccel: false, // Whether to use hardware acceleration
        className: 'spinner', // The CSS class to assign to the spinner
        zIndex: 2e9, // The z-index (defaults to 2000000000)
        top: 'auto', // Top position relative to parent in px
        left: 'auto' // Left position relative to parent in px
    };
    var spinner = new Spinner(opts);

    var directiveDefs = {
        priority: 0,
        templateUrl: 'html/wait.htm',
        replace: true,
        transclude: false,
        restrict: 'A',
        scope: false,
        compile: function compile(element, tAttrs, transclude) {
            return function link(scope, element, attrs) {                
                scope.$watch(attrs.rextoyWait, function (cur, old) {
                    if (old == cur) return;
                    if (cur) {
                        $(element).modal({
                            keyboard: false,
                            backdrop: 'static'
                        });
                        var target = document.querySelector('#wait-spin');
                        spinner.spin(target);
                    } else {
                        spinner.stop();
                        $(element).modal('hide');
                    }
                });
            }
        }
    };

    return directiveDefs;
});
